package com.crm.kz.common;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class SystemConfig {
    public static void setConfig(Context context){
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("user",GlobalVariable.user);
            jsonObject.put("password",GlobalVariable.password);
            jsonObject.put("serverAddress",GlobalVariable.serverAddress);
            FileOutputStream  fos = new FileOutputStream(context.getCacheDir()+"/sysconfig.config", false);
            fos.write(jsonObject.toString().getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getConfig(Context context){
        try {
            FileInputStream fis = new FileInputStream(context.getCacheDir()+"/sysconfig.config");
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            fis.close();
            String content = sb.toString();
            JSONObject jsonObject = JSON.parseObject(content);

            if(jsonObject.get("user") != null){
                GlobalVariable.user = (String) jsonObject.get("user");
            }
            if(jsonObject.get("password")!= null){
                GlobalVariable.password = (String) jsonObject.get("password");
            }
            if(jsonObject.get("serverAddress")!= null){
                GlobalVariable.serverAddress = (String) jsonObject.get("serverAddress");
            }



        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
